﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.ObjectBuilder2;
using WebStore.Domain.Entities.Enum;
using WebStore.Domain.Entities.Model;
using WebStore.Domain.Intializer.Extension;

namespace WebStore.Domain.Intializer
{
    public class EntitiesInitializer : DropCreateDatabaseIfModelChanges<Entities.Entities>
    {
        public Entities.Entities Entities { get; set; }
        protected override void Seed(Entities.Entities context)
        {
            Entities = context;
            Categories.ForEach(x => context.Categories.Add(x));
            context.SaveChanges();
            Products.ForEach(x=>context.Products.Add(x));
            context.SaveChanges();
            Roles.ForEach(x=>context.Roles.Add(x));
            context.SaveChanges();
            Users.ForEach(x=>context.Users.Add(x));
            context.SaveChanges();
            UserRoles.ForEach(x=>context.UserRoles.Add(x));
            context.SaveChanges();
            base.Seed(context);
        }

        private IEnumerable<Product> Products
        {
            get
            {
                return new Product[]
                           {
                               new Product
                                   {
                                       Name = "Football",
                                       Description = "This is ball",
                                       Price = 50,
                                       Category = Entities.Categories.FirstOrDefault(x=>x.Name=="Sport"),
                                       Photo = ResourceConverter.ImageToByte2(Resource.Ball)

                                   },
                                   new Product
                                   {
                                       Name = "Snowboard",
                                       Description = "The best snowboard",
                                       Price = 50,
                                       Category = Entities.Categories.FirstOrDefault(x=>x.Name=="Sport"),
                                       Photo = ResourceConverter.ImageToByte2(Resource.Snowboard)
                                   },
                                    new Product
                                   {
                                       Name ="Bike",
                                       Description = "The fastest bike",
                                       Price = 50,
                                       Category = Entities.Categories.FirstOrDefault(x=>x.Name=="Sport"),
                                       Photo = ResourceConverter.ImageToByte2(Resource.Bike)
                                   },
                                new Product
                                    {
                                        Name = "Apple",
                                        Description = "Red apple",
                                        Price = 5,
                                        Category = Entities.Categories.FirstOrDefault(x=>x.Name=="Food"),
                                        Photo = ResourceConverter.ImageToByte2(Resource.Apple)
                                    },
                                  new Product
                                    {
                                        Name = "Orange Juice",
                                        Description = "Orange juice",
                                        Price = 5,
                                        Category = Entities.Categories.FirstOrDefault(x=>x.Name=="Food"),
                                        Photo = ResourceConverter.ImageToByte2(Resource.Orange)
                                    },
                                new Product
                                    {
                                        Name = "Water",
                                        Description = "Crystal water",
                                        Price = 10,
                                        Category = Entities.Categories.FirstOrDefault(x=>x.Name=="Food"),
                                        Photo = ResourceConverter.ImageToByte2(Resource.Water)
                                    }
                           };
            }
        }
        private IEnumerable<Category> Categories
        {
            get
            {
                return new Category[]
                           {
                               new Category
                                   {
                                       Name = "Sport"
                                   },
                               new Category
                                   {
                                       Name = "Food"
                                   }
                           };
            }
        }
        private IEnumerable<Role>Roles
        {
            get
            {
                return new Role[]
                           {
                               new Role
                                   {
                                       RoleType = RoleType.Administrator
                                   }
                           };
            }
        }
        private IEnumerable<User> Users
        {
            get
            {
                return new User[]
                           {
                            new User
                                {
                                    Email = "admin@o2.pl",
                                    Password = "asd",
                                    FirstName = "Admin"
                                }
                           };
            }
        }
        private IEnumerable<UserRole> UserRoles
        {
            get
            {
                return new UserRole[]
                           {
                                new UserRole
                                    {
                                        RoleId = 1,
                                        UserId = 1
                                    }
                           };
            }
        } 
    }
}
